package entity

import (
	"context"
	"time"

	"github.com/LyricTian/gin-admin/v7/internal/app/schema"
	"github.com/LyricTian/gin-admin/v7/pkg/util/structure"
	"github.com/jinzhu/gorm"
)

// GetUserLoginDB 获取user_login存储
func GetUserLoginDB(ctx context.Context, defDB *gorm.DB) *gorm.DB {
	return GetDBWithModel(ctx, defDB, new(UserLogin))
}

// SchemaUserLogin user_login对象
type SchemaUserLogin schema.UserLogin

// ToUserLogin 转换为user_login实体
func (a SchemaUserLogin) ToUserLogin() *UserLogin {
	item := new(UserLogin)
	structure.Copy(a, item)
	return item
}

// UserLogin user_login实体
type UserLogin struct {
	ID        string     `gorm:"column:id;primary_key;size:36;"`
	UserId    string     `gorm:"size:36;index;column:user_id;"` // 用户唯一标识
	Type      int        `gorm:"column:type;size:11;type:int;"` // 登录方式(1:mobile 2:email 3:user_name 4:wx_wapp 5:wx_openid)
	Salt      string     `gorm:"size:60;column:salt;"`          // 密盐
	Key       string     `gorm:"size:255;column:key;"`          // Key
	Value     string     `gorm:"size:255;column:value;"`        // Value
	Status    int        `gorm:"column:status;size:11;"`        // 状态(0:未激活 1:已激活 2:账号锁定 3:账号停用)
	Creator   string     `gorm:"size:36;column:creator;"`       // 创建者
	CreatedAt time.Time  `gorm:"column:created_at;index;"`
	UpdatedAt time.Time  `gorm:"column:updated_at;index;"`
	DeletedAt *time.Time `gorm:"column:deleted_at;index;"`
}

// ToSchemaUserLogin 转换为user_login对象
func (a UserLogin) ToSchemaUserLogin() *schema.UserLogin {
	item := new(schema.UserLogin)
	structure.Copy(a, item)
	return item
}

// UserLogins user_login列表
type UserLogins []*UserLogin

// ToSchemaUserLogins 转换为user_login对象列表
func (a UserLogins) ToSchemaUserLogins() []*schema.UserLogin {
	list := make([]*schema.UserLogin, len(a))
	for i, item := range a {
		list[i] = item.ToSchemaUserLogin()
	}
	return list
}
